#!/bin/bash

PATH_LIB=$OSVC_PATH_COMP/com.replication-manager
PREFIX=OSVC_COMP_SPDC_SVC_DB_CNF

typeset -i r=0
typeset -i start=$OSVC_COMP_FIRST_SERVER_IP
typeset -i nservers=$OSVC_COMP_N_SERVERS

case $1 in
check)
	for pod in $(echo /$OSVC_COMP_SERVICES_SVCNAME/pod*)
	do
		export OSVC_COMP_POD=$(basename $pod)
		typeset -i n=$(echo $OSVC_COMP_POD | sed -e "s@pod[0]*@@")
		n=n-1

		export OSVC_COMP_SERVER_ID=$(($start+2*$n))
		export OSVC_COMP_PEER_SERVER_ID=$((($OSVC_COMP_SERVER_ID - $start + $nservers / 2) % $nservers + $start))
		$OSVC_PYTHON $PATH_LIB/file.py ${PREFIX}_FILE_SM check
		[ $? -eq 1 ] && r=1

		export OSVC_COMP_SERVER_ID=$(($start+2*$n+1))
		export OSVC_COMP_PEER_SERVER_ID=$((($OSVC_COMP_SERVER_ID - $start + $nservers / 2) % $nservers + $start))
		$OSVC_PYTHON $PATH_LIB/file.py ${PREFIX}_FILE_DB check
		[ $? -eq 1 ] && r=1
	done
	exit $r
	;;
fix)
	for pod in $(echo /$OSVC_COMP_SERVICES_SVCNAME/pod*)
	do
		export OSVC_COMP_POD=$(basename $pod)
		typeset -i n=$(echo $OSVC_COMP_POD | sed -e "s@pod[0]*@@")
		n=n-1

		export OSVC_COMP_SERVER_ID=$(($start+2*$n))
		export OSVC_COMP_PEER_SERVER_ID=$((($OSVC_COMP_SERVER_ID - $start + $nservers / 2) % $nservers + $start))
		$OSVC_PYTHON $PATH_LIB/file.py ${PREFIX}_FILE_SM fix
		[ $? -eq 1 ] && exit 1

		export OSVC_COMP_SERVER_ID=$(($start+2*$n+1))
		export OSVC_COMP_PEER_SERVER_ID=$((($OSVC_COMP_SERVER_ID - $start + $nservers / 2) % $nservers + $start))
		$OSVC_PYTHON $PATH_LIB/file.py ${PREFIX}_FILE_DB fix
		[ $? -eq 1 ] && exit 1
	done
	exit 0
	;;
fixable)
	exit 2
	;;
esac
